package com.treasure.hunt.main;

import android.Manifest;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.MainThread;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppInstallAdapter;
import com.fm.openinstall.model.AppData;
import com.module.lemlin.base.BaseAbstractActivity;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.treasure.hunt.R;
import com.treasure.hunt.app.AppConstance;
import com.treasure.hunt.config.TTAdManagerHolder;
import com.treasure.hunt.entity.SchemeData;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * 开屏广告Activity示例
 */
public class SplashActivity extends BaseAbstractActivity {
    private FrameLayout mSplashContainer;
    //是否强制跳转到主页面
    private boolean mForceGoMain;

    private LinearLayout mSplashHalfSizeLayout;

    private Boolean isPermissions = false;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {

        mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);
        mSplashHalfSizeLayout = (LinearLayout) findViewById(R.id.splash_half_size_layout);

        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Throwable {
                isPermissions = aBoolean;
                if (isPermissions) {
                    loadSplashAd();
                } else {
                    ToastUtils.showShort("请到设置中开启相关权限");
                    finish();
                }
            }
        });


        // 获取安装携带的参数
        OpenInstall.getInstall(new AppInstallAdapter() {
            @Override
            public void onInstall(AppData appData) {
                // 打印数据便于调试
                LogUtils.e("=====getInstall : installData = " + appData.toString());
                // 获取渠道数据
                String channelCode = appData.getChannel();
                // 获取自定义数据
                String bindData = appData.getData();

                SchemeData schemeData = null;
                try {
                    schemeData = GsonUtils.fromJson(bindData, SchemeData.class);
                } catch (Exception e) {
                    schemeData = null;
                }
                if (schemeData == null)
                    return;
                String type = schemeData.getType();
                if (type == null) {
                    return;
                }
                if (type.equals("1")) {
                    SPStaticUtils.put(AppConstance.KEY_NAME_PARENT_ID, schemeData.getCart_id());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        //判断是否该跳转到主页面
        if (mForceGoMain) {
            goToMainActivity();
        }
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mForceGoMain = true;
    }

    @Override
    protected void onDestroy() {
        if (mSplashContainer != null) {
            mSplashContainer.removeAllViews();
        }
        super.onDestroy();
    }

    /**
     * 加载开屏广告
     */
    private void loadSplashAd() {

        TTAdNative mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("887446785")
                .setImageAcceptedSize(1080, 1920)
                .build();
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            @MainThread
            public void onError(int code, String message) {
                LogUtils.e(String.valueOf(message));
                goToMainActivity();
            }

            @Override
            @MainThread
            public void onTimeout() {
                LogUtils.e("开屏广告加载超时");
                goToMainActivity();
            }

            @Override
            @MainThread
            public void onSplashAdLoad(TTSplashAd ad) {
                LogUtils.e("开屏广告请求成功");
                if (ad == null) {
                    return;
                }
                //获取SplashView
                View view = ad.getSplashView();
                if (view != null && mSplashContainer != null && !SplashActivity.this.isFinishing()) {
                    mSplashContainer.setVisibility(View.VISIBLE);
                    if (mSplashHalfSizeLayout != null) {
                        mSplashHalfSizeLayout.setVisibility(View.GONE);
                    }
                    mSplashContainer.removeAllViews();
                    mSplashContainer.addView(view);
                } else {
                    goToMainActivity();
                }


                //设置SplashView的交互监听器
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int type) {
                        LogUtils.e("开屏广告点击");
                    }

                    @Override
                    public void onAdShow(View view, int type) {
                        LogUtils.e("开屏广告展示");
                    }

                    @Override
                    public void onAdSkip() {
                        LogUtils.e("开屏广告跳过");
                        goToMainActivity();

                    }

                    @Override
                    public void onAdTimeOver() {
                        LogUtils.e("开屏广告倒计时结束");
                        goToMainActivity();
                    }
                });
            }
        }, 3000);

    }

    /**
     * 跳转到主页面
     */
    private void goToMainActivity() {
        if (isPermissions) {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            this.finish();
        } else {
            toast("权限未打开");
        }
    }
}
